package com.victor.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.victor.system.service.SysCityService;
import com.victor.common.dao.system.SysCityDao;
import com.victor.common.entity.system.SysCity;
import com.victor.common.response.system.CityResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 城市字典表 服务实现类
 * </p>
 *
 * @author benetech
 * @since 2022年12月19日
 */
@Service
public class SysCityServiceImpl extends ServiceImpl<SysCityDao, SysCity> implements SysCityService {

    @Override
    public List<CityResponse> getProvinceDictionary(String provinceCode) {
        List<SysCity> sysCities = getBaseMapper().selectList(new LambdaQueryWrapper<SysCity>()
                .select(SysCity::getProvinceCode, SysCity::getProvinceName)
                .eq(StringUtils.isNotBlank(provinceCode), SysCity::getProvinceCode, provinceCode)
                .groupBy(SysCity::getProvinceCode)
                .groupBy(SysCity::getProvinceName));
        List<CityResponse> responses = new ArrayList<>();
        sysCities.forEach(item -> {
                    CityResponse cityResponse = new CityResponse();
                    cityResponse.setCode(item.getProvinceCode());
                    cityResponse.setName(item.getProvinceName());
                    responses.add(cityResponse);
                }
        );
        return responses;
    }

    @Override
    public List<CityResponse> getCityDictionary(String provinceCode) {
        List<SysCity> sysCities = getBaseMapper().selectList(new LambdaQueryWrapper<SysCity>()
                .select(SysCity::getCityCode, SysCity::getCityName)
                .eq(StringUtils.isNotBlank(provinceCode), SysCity::getProvinceCode, provinceCode)
                .groupBy(SysCity::getCityCode)
                .groupBy(SysCity::getCityName));
        List<CityResponse> responses = new ArrayList<>();
        sysCities.forEach(item -> {
                    CityResponse cityResponse = new CityResponse();
                    cityResponse.setCode(item.getCityCode());
                    cityResponse.setName(item.getCityName());
                    responses.add(cityResponse);
                }
        );
        return responses;
    }

    @Override
    public List<CityResponse> getDistrictDictionary(String cityCode) {
        List<SysCity> sysCities = getBaseMapper().selectList(new LambdaQueryWrapper<SysCity>()
                .select(SysCity::getDistrictCode, SysCity::getDistrictName)
                .eq(StringUtils.isNotBlank(cityCode), SysCity::getCityCode, cityCode)
                .groupBy(SysCity::getDistrictCode)
                .groupBy(SysCity::getDistrictName));
        List<CityResponse> responses = new ArrayList<>();
        sysCities.forEach(item -> {
                    CityResponse cityResponse = new CityResponse();
                    cityResponse.setCode(item.getDistrictCode());
                    cityResponse.setName(item.getDistrictName());
                    responses.add(cityResponse);
                }
        );
        return responses;
    }

    @Override
    public List<CityResponse> getTownDictionary(String districtCode) {
        List<SysCity> sysCities = getBaseMapper().selectList(new LambdaQueryWrapper<SysCity>()
                .select(SysCity::getTownCode, SysCity::getTownName)
                .eq(StringUtils.isNotBlank(districtCode), SysCity::getDistrictCode, districtCode)
                .groupBy(SysCity::getTownCode)
                .groupBy(SysCity::getTownName));
        List<CityResponse> responses = new ArrayList<>();
        sysCities.forEach(item -> {
                    CityResponse cityResponse = new CityResponse();
                    cityResponse.setCode(item.getTownCode());
                    cityResponse.setName(item.getTownName());
                    responses.add(cityResponse);
                }
        );
        return responses;
    }

    @Override
    public List<CityResponse> getVillageDictionary(String townCode) {
        List<SysCity> sysCities = getBaseMapper().selectList(new LambdaQueryWrapper<SysCity>()
                .select(SysCity::getVillageCode, SysCity::getVillageName)
                .eq(StringUtils.isNotBlank(townCode), SysCity::getTownCode, townCode)
                .groupBy(SysCity::getVillageCode)
                .groupBy(SysCity::getVillageName));
        List<CityResponse> responses = new ArrayList<>();
        sysCities.forEach(item -> {
                    CityResponse cityResponse = new CityResponse();
                    cityResponse.setCode(item.getVillageCode());
                    cityResponse.setName(item.getVillageName());
                    responses.add(cityResponse);
                }
        );
        return responses;
    }
}
